from secrets import token_bytes

from blspy import AugSchemeMPL, PrivateKey, G1Element

from chia.plotting.plot_tools import stream_plot_info_pk
from chia.types.blockchain_format.proof_of_space import ProofOfSpace
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.hash import std_hash
from chia.wallet.derive_keys import master_sk_to_local_sk
import csv

num = 2000
fpk = 'ae2ccd2dbd20b878f8fd2c6e4aeea44f935d90f9bd346a14dbc7ec604733827af1f9d10c5b03b42b68822bb671d6f2c8'
ppk = 'b0c1b63255ad41a760cf64d965eee81c22b5d820f28a74f7c67bfbf6970628f71df8208651a3165351f818e0bcd72ee0'
test_private_keys = [
    AugSchemeMPL.key_gen(std_hash(i.to_bytes(2, "big")))
    for i in range(num)
]

csv_file = open('id.csv', 'w+', newline='', encoding='utf-8')
writer = csv.writer(csv_file)
rows = []
for i in range(num):
    # Generate a random master secret key
    if test_private_keys is not None:
        # assert len(test_private_keys) == num
        sk: PrivateKey = test_private_keys[i]
    else:
        sk = AugSchemeMPL.key_gen(token_bytes(32))
    farmer_public_key = G1Element.from_bytes(bytes.fromhex(fpk))
    pool_public_key = G1Element.from_bytes(bytes.fromhex(ppk))
    plot_public_key = ProofOfSpace.generate_plot_public_key(master_sk_to_local_sk(sk).get_g1(), farmer_public_key)
    plot_id: bytes32 = ProofOfSpace.calculate_plot_id_pk(pool_public_key, plot_public_key)
    plot_memo: bytes32 = stream_plot_info_pk(pool_public_key, farmer_public_key, sk)
    text = ['0x' + plot_memo.hex().replace(ppk, '').replace(fpk, ''), '0x' + plot_id.hex()]
    # print(text)
    rows.append(text)
writer.writerows(rows)
csv_file.close()
